package com.cms.bs.app.service.impl;

import com.alibaba.druid.util.StringUtils;
import com.alibaba.fastjson.JSONArray;
import com.avaje.ebean.Ebean;
import com.avaje.ebean.ExpressionList;
import com.avaje.ebean.PagedList;
import com.cms.bs.app.model.Article;
import com.cms.bs.app.service.IArticleService;
import com.cms.bs.app.service.base.BaseService;
import com.cms.bs.config.rs.PageRS;
import com.cms.bs.config.rs.RS;
import com.cms.bs.j2ee.annotation.Log;
import com.cms.bs.util.BaseTypeUtil;
import com.cms.bs.util.JsonUtil;
import com.cms.bs.util.Log4jUtil;
import org.springframework.stereotype.Service;

import java.util.Arrays;

/**
 * Created by jiang on 2016-09-28.
 */
@Service
public class ArticleService extends BaseService implements IArticleService {

    /**
     * 创建文章
     *
     * @param article
     * @return
     */
    @Override
    public RS create(Article article) {
        try {
            // 1.1 判断参数是否异常
            if (article == null || StringUtils.isEmpty(article.getTitle())) {
                return RS.bad("参数异常.");
            }
            // 1.2 保存
            Ebean.save(article);
            return RS.ok();
        } catch (Exception e) {
            Log4jUtil.error(this.getClass().getName(), "create article 异常：", e);
        }
        return RS.bad("创建文章记录异常.");
    }

    /**
     * 删除 文章
     *
     * @param id
     * @return
     */
    @Override
    public RS delete(String id) {
        try {
            if (id != null) {
                int delete = 0;
                if (id.contains(",")) {
                    delete = Ebean.deleteAll(Article.class, Arrays.asList(id.split(",")));
                } else {
                    delete = Ebean.delete(Article.class, id);
                }
                if (delete > 0) {
                    return RS.ok();
                }
                return RS.bad("文章不存在或已被删除.");
            }
            return RS.bad("参数错误.");
        } catch (Exception e) {
            Log4jUtil.error(this.getClass().getName(), "delete article 异常：", e);
        }
        return RS.bad("删除文章记录异常.");
    }

    /**
     * 修改 文章
     *
     * @param article
     * @return
     */
    @Override
    public RS update(Article article) {
        try {
            // 1.1 判断参数是否异常
            if (article == null || BaseTypeUtil.safeLong(article.getId()) == 0) {
                return RS.bad("参数异常.");
            }
            // 1.2 保存
            Ebean.update(article);
            return RS.ok();
        } catch (Exception e) {
            Log4jUtil.error(this.getClass().getName(), "update article 异常：", e);
        }
        return RS.bad("更新文章记录异常.");
    }

    /**
     * 获取 文章 详情
     *
     * @param id
     * @return
     */
    @Override
    public RS get(String id) {
        try {
        Article article = Ebean.find(Article.class).where().eq("id", id).findUnique();
            if (article != null) {
                return RS.ok(article);
            }
            return RS.bad("获取文章详情信息失败.");
        }catch (Exception e){
            Log4jUtil.error(this.getClass().getName(), "get article 异常：", e);
        }
        return RS.bad("获取文章详情异常.");
    }

    /**
     * 获取 文章列表： 分页
     *
     * @param article
     * @return
     */
    @Override
    public PageRS list(Article article) {
        try {
            ExpressionList<Article> expList = Ebean.find(Article.class).where();

            // 分页
            int pageIndex = 1;
            int pageSize = 100;

            if (article != null) {
                if (article.getPageIndex() != null) {
                    pageIndex = BaseTypeUtil.safeInt(article.getPageIndex());
                }
                if (article.getPageSize() != null) {
                    pageSize = BaseTypeUtil.safeInt(article.getPageSize());
                }
            }

            JSONArray params = JsonUtil.toJSONArray(article.getQueryParams());
            if (params != null && params.size() > 0) {
                expList.add(this.addQueryParam(params));
            }
            int rowStart = (pageIndex - 1) * pageSize;
            PagedList<Article> pagedList = expList.setFirstRow(rowStart).setMaxRows(pageSize).findPagedList();
            int totalRow = pagedList.getTotalCount();
            int totalPage = pagedList.getTotalPageCount();
            return PageRS.ok(pageIndex, pageSize, totalRow, totalPage, pagedList.getList());
        } catch (Exception e) {
            Log4jUtil.error(this.getClass().getName(), "list article 异常：", e);
        }
        return PageRS.bad("获取文章列表异常.");
    }

}
